package com.xiaohudie.SGGJUC;

import lombok.SneakyThrows;

import java.util.concurrent.*;

/**
 * @Author xiao hu die
 * @Date 2022/3/8 12:50
 * @Version 1.0
 */
public class TestCallable2 {
    @SneakyThrows
    public static void main(String[] args) {
        TestFutureTask2 testFutureTask2 = new TestFutureTask2();
        FutureTask<String> stringFutureTask = new FutureTask<>(testFutureTask2);
        CopyOnWriteArrayList<String> strings = new CopyOnWriteArrayList<>();
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i <10 ; i++) {
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    /**必须先调用run方法才能实现这个线程*/
                    stringFutureTask.run();
                   // System.out.println(Thread.currentThread().getName()+stringFutureTask.get());
                  strings.add(stringFutureTask.get());
                 countDownLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(strings);
    }
}
class TestFutureTask2 implements Callable<String> {

    @Override
    public String call() throws Exception {
        return "陈唯怡";
    }
}
